﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Command;
using JustinAngel.Neurons.Framework;
using JustinAngel.Neurons.Framework.Helpers;
using JustinAngel.Neurons.Pages.Main;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Primitives;

namespace JustinAngel.Neurons
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            this.mainPanorama.Background = PanoramaBackground;

        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= new RoutedEventHandler(MainPage_Loaded);
            if (this.DataContext == null)
                this.DataContext = new ViewModelLocator().MainPageViewModel;
        }

        //void MainPage_Loaded(object sender, RoutedEventArgs e)
        //{
        //    var title = mainPanorama.GetVisualChild<PanningTitleLayer>();
        //    var transform = title.RenderTransform.As<MatrixTransform>();
        //    //var transforms = title.RenderTransform.As<TransformGroup>();
        //    //var translate = transforms.Children.OfType<TranslateTransform>().Single();
        //    //MessageBox.Show(translate.X.ToString());k
        //    Debug.WriteLine(transform.Matrix.IsIdentity.ToString()); 
        //}

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            State["PanoramaSelectedItem"] = mainPanorama.SelectedIndex;
            //State["PanoramaSelectedItemVerticalOffset"] = 
            //    mainPanorama.SelectedItem.As<PanoramaItem>().Content.As<ScrollViewer>().VerticalOffset;

        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (State.ContainsKey("PanoramaSelectedItem"))
            {
                var selIndex = (int)State["PanoramaSelectedItem"];
                //var verticalOffset = (double) State["PanoramaSelectedItemVerticalOffset"];
                //mainPanorama.Items[selIndex].As<PanoramaItem>().Content.As<ScrollViewer>().ScrollToVerticalOffset(verticalOffset);
                mainPanorama.DefaultItem = mainPanorama.Items[selIndex];
            }
        }

        /// <summary>
        /// This is the world's ugliest hack for not being able to use Buttons in ListBoxItem.
        /// So we get this generic event and convert it to a real command. 
        /// </summary>
        public void ListBoxSelecitonChangedFireRealItemCommand(object sender, SelectionChangedEventArgs e)
        {
            //if (e.AddedItems.Count != 0)
            //{

            //    var listBoxFiringTheEvent = (ListBox)sender;

            //    var clickedItem = (VideoItemViewModel)e.AddedItems[0];

                
            //    clickedItem.NavigateToVideo.Execute(null);

            //    listBoxFiringTheEvent.SelectedItem = null;
            //}
        }

        public ImageBrush PanoramaBackground
        {
            get
            {
                return new ImageBrush()
                {
                    ImageSource = new BitmapImage(PanoramaBackgroundImageSource),
                    Stretch = Stretch.None
                };
            }
        }

        public Uri PanoramaBackgroundImageSource
        {
            get
            {
                Visibility PhoneLightThemeVisibility = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];
                if (PhoneLightThemeVisibility == Visibility.Collapsed)
                {
                    return new Uri("/PanoramaBackgroundNeuron.jpg", UriKind.Relative);
                }
                else
                {
                    return new Uri("/PanoramaBackgroundNeuronLight.jpg", UriKind.Relative);
                }
            }
        }
    }
}